package cn.youfule.dp.iterator;

public class LinkedList implements Collection{

	private Node head = null;
	private Node tail = null;
	private int size = 0;
	
	public void add(Object o) {
		Node n = new Node(o,null);
		if(head == null) {
			head = n;
			tail = n;
		}
		tail.setNext(n);
		tail = n;
		size ++;
	}
	
	public int size() {
		return size;
	}

	@Override
	public Iterator iterator() {
		return new LinkedListIterator();
	}
	
	private class LinkedListIterator implements Iterator{

		private Node current = head;
		
		@Override
		public Object next() {
			Node next = current;
			if(next != null) {
				current = current.getNext();
				return next.getData();
			}
			return null;
		}

		@Override
		public boolean hasNext() {
			return current != null;
		}
		
	}
}
